/**
 * @file store
 */

import { createStore } from 'vuex';
import {
  WEBINAR_STATUS_LIVE,
  WEBINAR_STATUS_NOTICE,
  WEBINAR_STATUS_OVER,
  WEBINAR_STATUS_BACK,
  WEBINAR_STATUS_INIT_OVER,
  WEBINAR_STATUS_OVER_AGREEMENT,
  WEBINAR_STATUS_OVER_TO_LIVE,
  PAGE_ROOM_IN,
  REMOTE_STREAM_SCREEN_SHARE,
  REMOTE_STREAM_INTERSTITIAL,
  WEBINAR_MODE_AUDIO,
  WEBINAR_MODE_VIDEO,
  WEBINAR_MODE_INTERACTION
} from 'src/common/const/status';

export default createStore({
  state: {
    zIndex: 2000,
    jssdkInstance: null, // sdk实例
    user: {}, // 用户信息
    pageStatus: PAGE_ROOM_IN, // 状态
    // 房间信息
    room: {
      isOpenDocument: false, // 是否开启文档共享
      isSubscribeSpecial: false, // 是否订阅特殊流
      isSubscribeHost: false, // 是否订阅主持人流
      isHand: false, // 是否在麦
      isPlayerMainScreen: true, // 是否播放器在大屏上
      status: WEBINAR_STATUS_LIVE, // 活动状态
      mode: '', // 活动模式
      isDelay: true // 是否是延迟直播
    },
    isModuleReady: false,
    // 特殊流
    specialStream: {
      [REMOTE_STREAM_SCREEN_SHARE]: '', // 桌面共享
      [REMOTE_STREAM_INTERSTITIAL]: '' // 插播
    },
    hostStreamInfo: {}, // 主持人流信息
    // 聊天
    chat: {
      isDisabled: false, // 是否禁言
      isOpenQuestion: false // 是否开启问答
    },
    // 本地
    local: {
      isMainScreen: false, // 是否本地流为主画面
      video: true, // 摄像头-开启
      audio: true // 麦克风-开启
    },
    //章节
    chapters: {
      isOpenDocVodChapters: false, //是否开启章节
      chaptersList: [] //章节数据
    },
    agreement: {
      is_agree: 0,
      is_open: 0
    },
    // loading
    loading: {
      isShow: false, // 是否加载loading
      isMask: false // 是否显示蒙层
    },
    msgQuestionnaire: {}, // 消息-问卷
    isOpenQuestionnaire: false, // 是否打开问卷
    // 推屏卡片
    screenCard: {
      info: {}, // 卡片信息
      isOpen: false // 是否打开
    },
    // 推送商品卡
    product: {
      info: {}, // 当前推送的商品信息
      isOpen: false // 是否打开
    },
    productList: [], // 商品列表
    mainStream: '' //主画面流
  },
  getters: {
    // 是否-无法正常进入房间
    isGlobalError(state) {
      return state.pageStatus !== PAGE_ROOM_IN;
    },
    // 直播-进行中
    isLive(state) {
      return state.room.status === WEBINAR_STATUS_LIVE;
    },
    // 直播-预约中
    isNotice(state) {
      return (
        state.room.status === WEBINAR_STATUS_NOTICE ||
        (state.agreement.is_open && state.agreement.is_agree == 0)
      );
    },
    // 直播-已结束
    isOver(state) {
      return state.room.status === WEBINAR_STATUS_OVER;
    },
    // 直播-回放
    isBack(state) {
      return state.room.status === WEBINAR_STATUS_BACK;
    },
    // 初始化-已结束
    isInitOver(state) {
      return state.room.status === WEBINAR_STATUS_INIT_OVER;
    },
    // 已结束转直播
    isOverToLive(state) {
      return state.room.status === WEBINAR_STATUS_OVER_TO_LIVE;
    },
    // 是否订阅桌面共享流
    isSubscribeShare(state) {
      return state.specialStream[REMOTE_STREAM_SCREEN_SHARE];
    },
    // 是否是音频活动
    isModeAudio(state) {
      return state.room.mode === WEBINAR_MODE_AUDIO;
    },
    // 是否是视频活动
    isModeVideo(state) {
      return state.room.mode === WEBINAR_MODE_VIDEO;
    },
    // 是否是互动活动
    isModeinteraction(state) {
      return state.room.mode === WEBINAR_MODE_INTERACTION;
    },
    //观看协议
    getAgreement(state) {
      return state.agreement;
    },
    getStatus(state) {
      return state.room.status;
    },
    getMainStream(state) {
      return state.mainStream;
    }
  },
  mutations: {
    //设置模块挂载状态
    setModuleReady(state, status) {
      state.isModuleReady = status;
    },
    // 设置用户
    setUser(state, user) {
      state.user = user;
    },
    // 设置实例对象
    setJSSDK(state, jssdkInstance) {
      state.jssdkInstance = jssdkInstance;
    },
    //观看协议
    setAgreement(state, agreement) {
      state.agreement = agreement;
    },
    // 设置页面状态
    setPageStatus(state, status) {
      state.pageStatus = status;
    },
    // 设置房间信息
    setRoom(state, room) {
      console.log('setRoom-------', room);
      state.room = {
        ...state.room,
        ...room
      };
    },
    // 设置聊天
    setChat(state, chat) {
      state.chat = {
        ...state.chat,
        ...chat
      };
    },
    //设置章节
    setChapters(state, isOpenDocVodChapters) {
      state.chapters.isOpenDocVodChapters = isOpenDocVodChapters;
    },
    //设置章节数据
    setChaptersData(state, chaptersList) {
      state.chapters.chaptersList = chaptersList;
    },
    // 设置主持人流
    setHostStreamInfo(state, hostStreamInfo) {
      state.hostStreamInfo = hostStreamInfo;
    },
    // 设置特殊流
    setSpecialStream(state, specialStream) {
      state.specialStream = {
        ...state.specialStream,
        ...specialStream
      };
    },
    // 设置本地信息
    setLocal(state, local) {
      state.local = {
        ...state.local,
        ...local
      };
    },
    // 设置loading信息
    setLoading(state, loading) {
      state.loading = {
        ...state.loading,
        ...loading
      };
    },
    // 直播结束-重置
    resetOverStatus(state) {
      state.room.isOpenDocument = false;
      state.room.isHand = false;
      state.room.isPlayerMainScreen = true;
      state.chat.isDisabled = true;
    },
    // 下麦-重置
    resetOutStatus(state) {
      state.room.isHand = false;
      state.hostStreamInfo = {};
      state.room.isSubscribeHost = false;
    },
    // 设置-问卷信息
    setMsgQuestionnaire(state, msgQuestionnaire) {
      state.msgQuestionnaire = msgQuestionnaire;
    },
    // 设置-是否打开问卷
    setIsOpenQuestionnaire(state, isOpen) {
      state.isOpenQuestionnaire = isOpen;
    },
    // 设置-是否打开推屏卡片
    setScreenCard(state, screenCard) {
      state.screenCard = {
        ...state.screenCard,
        ...screenCard
      };
    },
    // 设置-商品信息
    setProduct(state, product) {
      state.product = Object.assign(state.product, product);
      if (!product.isOpen) {
        sessionStorage.removeItem(`product_${state.jssdkInstance?.config?.roomid}`);
      } else {
        sessionStorage.setItem(
          `product_${state.jssdkInstance?.config?.roomid}`,
          JSON.stringify(state.product)
        );
      }
    },
    // 设置商品列表
    setProductList(state, data) {
      let list = data
        .filter(item => item.status == 1)
        .map(item => {
          return {
            ...item,
            newPrice: item.discount_price ? item.discount_price : item.price
          };
        });
      state.productList = list;
    },
    // 自增层级
    plusZIndex(state) {
      state.zIndex++;
    },
    setMainStream(state, stream) {
      state.mainStream = stream;
    }
  }
});
